#include <stdio.h>
int main()
{
    int k, i, j, n, len, d, count;
    char ch[10001];
    scanf("%d",&k);
    getchar();
    while(k--)
    {
        scanf("%s",ch);
        scanf("%d", &n);
        while(n--)
        {
            count = 0;
            scanf("%d %d",&i, &j);
            for(d = i-1; d < j-1;d++)
            {
                if(ch[d] == 'M')
                {
                    if(ch[d+1]=='F') count++;
                }
                else
                {
                    if(ch[d+1]=='M') count++;
                }
            }
            printf("%d\n",count);
        }
    }
}
